<?php

class Admin extends Controller {

	function Admin(){
		parent::Controller();
		$this->load->scaffolding('verses');
		$this->load->helper('url');
		$this->load->helper('form');
	}

	function abc() {
	echo('hh');
	}


	function insertflow(){
		$content='';
		if ($_POST) {
			$content=$_POST['content'];
		}


		$contentarray=explode("\n",$content);
	//	$content=implode("<br />",$carray);
		$content='';
		$prechapternum=0;
		$presectionnum=0;
		
/* 中文书卷名数组，全名、短名、章数 */
		$strBooks='创世记,创,出埃及记,出,利未记,利,民数记,民,申命记,申,约书亚记,书,士师记,士,路得记,得,撒母耳记上,撒上,撒母耳记下,撒下,列王记上,王上,列王记下,王下,历代志上,代上,历代志下,代下,以斯拉记,拉,尼希米记,尼,以斯帖记,斯,约伯记,伯,诗篇,诗,箴言,箴,传道书,传,雅歌,歌,以赛亚书,赛,耶利米书,耶,耶利米哀歌,哀,以西结书,结,但以理书,但,何西阿书,何,约珥书,珥,阿摩司书,摩,俄巴底亚书,俄,约拿书,拿,弥迦书,弥,那鸿书,鸿,哈巴谷书,哈,西番雅书,番,哈该书,该,撒迦利亚,亚,玛拉基书,玛,马太福音,太,马可福音,可,路加福音,路,约翰福音,约,使徒行传,徒,罗马书,罗,哥林多前书,林前,哥林多后书,林后,加拉太书,加,以弗所书,弗,腓立比书,腓,歌罗西书,西,帖撒罗尼迦前书,帖前,帖撒罗尼迦后书,帖后,提摩太前书,提前,提摩太后书,提后,提多书,多,腓利门书,门,希伯来书,来,雅各书,雅,彼得前书,彼前,彼得后书,彼后,约翰一书,约一,约翰二书,约二,约翰三书,约三,犹大书,犹,启示录,启';
		$strBooksCNum=Array(50,40,27,36,34,24,21,4,31,24,22,25,29,36,10,13,10,42,150,31,12,8,66,52,5,48,12,14,3,9,1,4,7,3,3,3,2,14,4,28,16,24,21,28,16,16,13,6,6,4,4,5,3,6,4,3,1,13,5,5,3,5,1,1,1,22);
		$aryBooksTemp=explode(',',$strBooks);
		
		for ($i = 1; $i <= 66; $i++) {
			$aryBooks[$i]=array('fn'=>$aryBooksTemp[($i-1)*2],'sn'=>$aryBooksTemp[($i-1)*2+1]
			,'c'=>$strBooksCNum[$i-1]
			);
		}

		$currentCatch=66;
	    echo($aryBooks[$currentCatch]['fn'].' '.$aryBooks[$currentCatch]['c'].'<br />');

		foreach($contentarray as $line):
		
			$linearray=explode('  ',$line);
			if (trim($linearray[0])==$aryBooks[$currentCatch]['sn']):			
				$ins['book_num']=$currentCatch;

				$ins['ver_id']=1;
				$numarray=explode(':',trim($linearray[1]));
				$ins['chapter_num']=$numarray[0];
				$ins['section_num']=$numarray[1];
				$ins['content']=trim($linearray[2]);

				if (isset($linearray[3])):
					$ins['content'].=trim($linearray[3]);
				endif;
				if (isset($linearray[4])):
					$ins['content'].=trim($linearray[4]);
				endif;

				
				if (($ins['chapter_num']!=$prechapternum)&&($ins['chapter_num']!=$prechapternum+1)):
					$content.='<font color=red>';
				endif;

				if (($ins['section_num']!=1)&&($ins['section_num']!=$presectionnum+1)):
					$content.='<font color=red>';
				endif;
				
			
				$prechapternum=$ins['chapter_num'];
				$presectionnum=$ins['section_num'];

				$content = $content.$ins['chapter_num'].' | '.$ins['section_num'].' | '.$ins['content'].'<br />';
				
		//		$this->db->insert('sections',$ins);

			endif;
			
		endforeach;


		$data['content']=$content;
		$this->load->view('insertflow_view',$data);
	}


	function index(){
		$data['title']="my blog title";
		$data['heading']="my blog heading";
		$data['query']=$this->db->get('sections');
		
		$this->load->view('blog_view',$data);
	}

	function search(){
/*
		$data['title']="my blog title";
		$data['heading']="my blog heading";
		$data['query']=$this->db->get('sections');
		
		$this->load->view('blog_view',$data);
*/
		if (preg_match('/([^\/\d]*)((\d*))?(\:(\d*)?)?$/', uri_string(), $matches)):
//			query=
//			echo($matches[1].'|'.$matches[2].'|'.$matches[5]);

			$this->db->from('books');
			$this->db->join('sections', 'sections.book_num = books.book_num');
			$this->db->where('books.book_name_short',$matches[1]);

			if (isset($matches[2])):
				$this->db->where('sections.chapter_num',$matches[2]);
			else:
				$this->db->where('sections.chapter_num',1);
			endif;
			if (isset($matches[5])):
				$this->db->where('sections.section_num',$matches[5]);
			endif;
			$query = $this->db->get();
			
			$data['query']=$query;
		endif;

		$data['uri_arg']=uri_string();
		
		$this->load->view('bible_read',$data);

//		$data['query']=;
	}


	function comments(){
		$data['title']="my comments title";
		$data['heading']="my comments heading";
		$this->db->where('entry_id', $this->uri->segment(3));
		$data['query']=$this->db->get('comments');
		
		$this->load->view('comment_view',$data);
	}

	function comment_insert(){
		$this->db->insert('comments',$_POST);
		redirect('blog/comments/'.$_POST['entry_id']);
	}
}

?>